import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBException;


public class TextHandling {
	public static String name=null;
	public static String nameFormatted=null;
//    private final static String newline = "\n";
    public static String notes = "c.d.ef.g.a.b";
    public static boolean keySet = false;
    public static boolean tempoSet = false;
    public static boolean newLearn = true;
	public static void receive(String text) throws JAXBException, IOException{

//        Main.textGUI.textArea.setText("length = "+ ((Integer) text.length()).toString() + newline);
        text=text.trim();
        text=text.toLowerCase();
        int textLength = text.length();
        if (Main.score.following)
        {
        	Integer number = Integer.parseInt(text.substring(0,2));
        	if (number < 65)
        	{
        		Main.score.malletLoc0=number;
        	}
        	else if (number > 75)
        	{
        		Main.score.malletLoc1=number;
        	}
        }
        else
        {
	        if (text.length()==1)
	        {
		        if (text.substring(0,1).equals("1")){
		        	Main.score.clearScheduler(1);
		        	Main.score.waitMil(500.);
		        	Main.score.melody1(72,16,16,2,1);
	
		        	Main.score.noteOut(95,0,3);
		        }
		        if (text.substring(0,1).equals("2")){
		    		Main.score.melody2(72,20,20,2,1);
		        }
	//	        if (text.substring(0,1).equals("3")){
	//	    		Main.score.melody3(72,7,7,2,1);
	//	        }
	//	
		        if (text.substring(0,1).equals("4")){
		    		Main.score.melody4(83,21,21,2,1);
		        }
	//	        if (text.substring(0,1).equals("5")){
	//	    		Main.score.melody5(76,16,16,2,1);
	//	        }
	//	        if (text.substring(0,1).equals("6")){
	//	    		Main.score.melody6(64,8,8,0,0);
	//	        }
	//	        if (text.substring(0,1).equals("8")){
	//	    		Main.score.melody8(60,44,44,2,1);
	//	        }
		        if (text.substring(0,1).equals("9")){
		    		Main.score.melody9(72,18,18,2,1);
	//	    		Main.score.melody9(60,18,18,2,1);
		        }
		        
	        }
	        if (text.length()>=2)
	        {
	        	if  (text.substring(0,2).equals("11")){
	        		Main.score.melody11(55,16,16,1,2);
	        	}

	//        	if (text.substring(0,2).equals("12")){
	//	    		System.out.println("goto melody2?");
	//	    		Main.score.melody12(81,16,16,2,1);
	//
	//	        }
		        if (text.substring(0,2).equals("13")){
		    		Main.score.melody13(67,16,16,2,1);
		        }
		        if (text.substring(0,2).equals("14")){
		        	Main.score.melody14(48,7,7,0,0);
		        }
		        if (text.substring(0,2).equals("15")){
		        	Main.score.melody15(48,16,16,0,0);
		        }
		        if (text.substring(0,2).equals("16")){
	//	    		Main.score.noteOut(72,0,2);
	//	    		Main.play.melody(16);
//		        	Main.score.melody16(60,16,16,0,1);		        
		        	Main.score.melody16(48,16,16,0,1);
	
		        }
		        if (text.substring(0,2).equals("17")){
		        	Main.score.melody14(84,7,7,2,1);
		        }
		        if (text.substring(0,2).equals("18")){
		        	Main.score.melody15(72,16,16,2,1);
		        }
		        if (text.substring(0,2).equals("19")){
		        	Main.score.melody16_2(84,16,16,2,1);
		        }
	        }
        }
        if (text.equals("clear all"))
        {
        	Main.score.playing0=0;
        	Main.score.playing1=0;
        	Main.score.clearScheduler();
        }
        if(text.equals("home"))
        {
    		PositionArms pos = new PositionArms();
    		Thread posThread = new Thread(pos);
    		posThread.start();
        }
        
        if (text.equals("follow")){
        	Main.score.following=true;
    		Follow follow=new Follow();
    		Thread playbackThread = new Thread(follow);
    		playbackThread.start();
      }

//    	if (text.substring(0,4).equals("play")){
//    		// run the score 
//
////    		try {
////    			Main.score.run();
////    		} catch (InterruptedException e) {
////    			e.printStackTrace();
////    		}
//    		Main.score=new Score();
//    		Thread playbackThread = new Thread(Main.score);
//    		playbackThread.start();
//    	}
    
          
        Main.textGUI.textField.selectAll();
        //Make sure the new text is visible, even if there
        //was a selection in the text area.
        Main.textGUI.textArea.setCaretPosition(Main.textGUI.textArea.getDocument().getLength());
	}
	
    public static int getNum(String content)
    {
    	int value=0;
		String regex = "([0-9])([0-9])([0-9])"; 
		Matcher m = Pattern.compile(regex).matcher(content); 
		if (m.find()){
			System.out.println("Please enter a strength argument as an integer from 1 to 99");
		}
		else {
			regex = "([0-9])([0-9])"; 
			m = Pattern.compile(regex).matcher(content);
			if (m.find()){
				value=Integer.parseInt(m.group(0));
			}
			else {
				regex = "([0-9])"; 
				m = Pattern.compile(regex).matcher(content);
				if (m.find()){
					value=Integer.parseInt(m.group(0));
				}
				else {
					System.out.println("Please enter a strength argument as an integer from 1 to 99");
				}
				
			}
		}
      return (value);
    }
}
